package com.zhitan.ems.app.stat.controller;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhitan.ems.app.stat.facade.StatFacade;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.extra.utils.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 统计数据表
 * @author zhitan
 * @date 2025-09-19 22:59:37
 */
@Api(tags = "统计数据表", value = "统计数据表")
@RestController
@RequestMapping("intra/stat/home")
public class StatisticsHomeController extends BaseController{
    @Autowired
    private StatFacade statFacade;
    @ApiOperation(value = "首页统计汇总数据", response = Map.class)
    @GetMapping("/statisticsSummary")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsSummary(@RequestParam("statTime") String statTime,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHomeHourlyStatistics(statTime,energyType);
    	return R.ok(result);
    }
    @ApiOperation(value = "首页设备耗能排行榜", response = Map.class)
    @GetMapping("/statisticsRanking")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsRanking(@RequestParam("statTime") String statTime,@RequestParam(value="energyType",required = false) Integer energyType,@RequestParam(value="top",required = false) Integer top) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	if(top==null) {
    		top=5;
    	}
    	result = statFacade.getHourlyStatisticsRanking(statTime,energyType,top);
    	return R.ok(result);
    }
    @ApiOperation(value = "Device统计汇总数据", response = Map.class)
    @GetMapping("/statisticsDevice")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsDevice(@RequestParam("statTime") String statTime,@RequestParam("deviceCode") String deviceCode,@RequestParam(value="address",required = false) String address,@RequestParam(value="groupId",required = false) Long groupId,@RequestParam(value="groupType" ,required = false) Integer groupType) throws ParseException {
        Map<String, Object> result = new HashMap<>();
		if(address==null) {
			address="volume";
		}
		 result = statFacade.listHourlyStatistics(statTime,deviceCode,address,groupId,groupType);
        return R.ok(result);
    }
    @ApiOperation(value = "Group统计汇总数据", response = Map.class)
    @GetMapping("/statisticsGroup")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsGroup(@RequestParam("statTime") String statTime,@RequestParam("groupId") Long groupId,@RequestParam(value="groupType" ,required = false) Integer groupType) throws ParseException {
        Map<String, Object> result = new HashMap<>();
        String deviceCode=null;
        String address=null;
        Integer statTimeType=3;
		if (statTime == null) {
		} else if (statTime.length() == 10) {
			statTimeType=2;
		} else if (statTime.length() == 7) {
			statTimeType=3;
		} else if (statTime.length() == 4) {
			statTimeType=4;
		}
        if(statTimeType != null && statTimeType == 1) {//小时
            // 小时统计
            result = statFacade.list24HourlyStatistics(statTime,deviceCode,address,groupId,groupType);
        } else if(statTimeType != null && statTimeType == 2) { //日
            // 天统计
            result = statFacade.list30DailyStatistics(statTime,deviceCode,address,groupId,groupType);
        } else if(statTimeType != null && statTimeType == 3) {//月
            // 月统计
            result =statFacade.list12MonthlyStatistics(statTime,deviceCode,address,groupId,groupType);
        } else if(statTimeType != null && statTimeType == 4) {//年
            // 年统计
            result = statFacade.list3YearlyStatistics(deviceCode,address,groupId,groupType);
        }
        return R.ok(result);
    }
    
    @ApiOperation(value = "Group占比汇总", response = Map.class)
    @GetMapping("/statisticsGroupPie")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsGroupPie(
    		@RequestParam("statTime") String statTime,@RequestParam(value="groupType" ,required = false) Integer groupType,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
        Map<String, Object> result = new HashMap<>();
		 result = statFacade.getHourPieStatistics(statTime,groupType,energyType);
        return R.ok(result);
    }
    @ApiOperation(value = "能源统计分析：同比环比:按小时，用能结构统计ByGroupId", response = Map.class)
    @GetMapping("/getHourlyStatisticsByGroupId")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourlyStatisticsByGroupId(@RequestParam("statTime") String statTime,@RequestParam(value="groupId" ,required = false) Long groupId,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourlyStatisticsByGroupId(statTime,groupId,energyType);
    	return R.ok(result);
    }
    @ApiOperation(value = "能源深度分析", response = Map.class)
    @GetMapping("/getHourlyStatisticsByGroupAndDeviceCode")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourlyStatisticsByGroupAndDeviceCode(@RequestParam("statTime") String statTime,@RequestParam(value="groupId" ,required = false) Long groupId,@RequestParam(value="energyType",required = false) Integer energyType,@RequestParam(value="groupType" ,required = false) Integer groupType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourlyStatisticsByGroupAndDeviceCode(statTime,groupId,energyType,groupType);
    	return R.ok(result);
    }
    @ApiOperation(value = "能源深度分析:碳排放分析", response = Map.class)
    @GetMapping("/getHourlyStatisticsCarbon")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourlyStatisticsCarbon(@RequestParam("statTime") String statTime,@RequestParam(value="groupId" ,required = false) Long groupId) throws ParseException {
    	Map<String, Object> result = new HashMap<>(); 
    	result = statFacade.getHourlyStatisticsCarbon(statTime,groupId);
    	return R.ok(result);
    }
    @ApiOperation(value = "能源深度分析:流向分析", response = Map.class)
    @GetMapping("/flowAnalysis")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> flowAnalysis(@RequestParam("statTime") String statTime,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.flowAnalysis(statTime,energyType);
    	return R.ok(result);
    }
    @ApiOperation(value = "能源深度分析:水电，气体，按小时，用能结构统计", response = Map.class)
    @GetMapping("/getHourListStatistics")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourListStatistics(@RequestParam("statTime") String statTime,@RequestParam(value="groupType" ,required = false) Integer groupType,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourListStatistics(statTime,groupType,energyType);
    	return R.ok(result);
    }
    @ApiOperation(value = "能源深度分析:成本分析", response = Map.class)
    @GetMapping("/getHourDeepStatCost")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourDeepStatCost(@RequestParam("statTime") String statTime,@RequestParam(value="groupId" ,required = false) Long groupId,@RequestParam(value="energyType",required = false) Integer energyType,@RequestParam(value="groupType" ,required = false) Integer groupType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourDeepStatCost(statTime,groupId,groupType,energyType);
    	return R.ok(result);
    }
    @ApiOperation(value = "成本汇总数据", response = Map.class)
    @GetMapping("/statisticsCost")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> statisticsCost(@RequestParam("statTimeType") Integer statTimeType,@RequestParam("statTime") String statTime,@RequestParam(value="deviceCode",required = false) String deviceCode,@RequestParam(value="groupId",required = false) Long groupId,@RequestParam(value="groupType" ,required = false) Integer groupType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	String address="volume";
    	if(statTimeType != null && statTimeType == 1) {//小时
    		// 小时统计
    		result = statFacade.list24HourlyStatistics(statTime,deviceCode,address,groupId,groupType);
    	} else if(statTimeType != null && statTimeType == 2) { //日
    		// 天统计
    		result = statFacade.list30DailyStatistics(statTime,deviceCode,address,groupId,groupType);
    	} else if(statTimeType != null && statTimeType == 3) {//月
    		// 月统计
    		result =statFacade.list12MonthlyStatistics(statTime,deviceCode,address,groupId,groupType);
    	} else if(statTimeType != null && statTimeType == 4) {//年
    		// 年统计
    		result = statFacade.list3YearlyStatistics(deviceCode,address,groupId,groupType);
    	}
    	return R.ok(result);
    }
    @ApiOperation(value = "成本看板:按小时，能源分类 汇总数据 能源成本堆叠趋势分析", response = Map.class)
    @GetMapping("/getHourStatisticsTypeGroupListStatistics")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourStatisticsTypeGroupListStatistics(@RequestParam("statTime") String statTime) throws ParseException {
    	 Map<String, Object> result = new HashMap<>();
		result = statFacade.getHourEnergyTypeGroupListStatistics(statTime);
    	return R.ok(result);
    }
    @ApiOperation(value = "成本看板:能源成本明细", response = Map.class)
    @GetMapping("/getHourStatisticsTypePieStatistics")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourStatisticsTypePieStatistics(@RequestParam("statTime") String statTime) throws ParseException {
    	 Map<String, Object> result = new HashMap<>();
		result = statFacade.getHourEnergyTypePieStatistics(statTime);
    	return R.ok(result);
    }
    
    @ApiOperation(value = "成本看板:能源消费整体汇总", response = Map.class)
    @GetMapping("/getHourlyStatisticsCost")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourlyStatisticsCost(@RequestParam("statTime") String statTime,@RequestParam(value="energyType",required = false) Integer energyType) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourlyStatisticsCost(statTime,energyType);
    	return R.ok(result);
    }
   
    @ApiOperation(value = "首页：成本看板:电力峰谷时段成本分布", response = Map.class)
    @GetMapping("/getHourElectricityCostTypePieStatistics")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourElectricityCostTypePieStatistics(@RequestParam("statTime") String statTime) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
		result = statFacade.getHourElectricityCostTypePieStatistics(statTime);
    	return R.ok(result);
    }
    @ApiOperation(value = "成本看板:按小时，电力峰谷时段成本分析", response = Map.class)
    @GetMapping("/getHourElectricityCostTypeListStatistics")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> getHourElectricityCostTypeListStatistics(@RequestParam("statTime") String statTime) throws ParseException {
    	Map<String, Object> result = new HashMap<>();
    	result = statFacade.getHourElectricityCostTypeListStatistics(statTime);
    	return R.ok(result);
    }

}